$.ajaxSetup({
    async : false
});

function initClassName() {
    $.get("http://139.199.200.112/dtpr/index.php?exam-teach-basic-classlist",function (data) {
        data=JSON.parse(data).data;
        for(var i=0;i<data.length;i++){
            createClass(data[i].id,data[i].name);
        }
    });
}

initClassName();

//选择一个班级
function selectClass(id,name) {
    //显示主界面
    $(".col-xs-10").show();

    $("#change-group-name").val(name);
    $("#class-id").text(id);
    $("#class-name").text(name);

    //获取学生信息并显示
    $.get("http://139.199.200.112/dtpr/index.php?exam-teach-basic-studentlist",{"id":id},function (data) {
        data=JSON.parse(data).data;
        //清空界面中的学生
        $("#stu-members").empty();
        for(var i=0;i<data.length;i++){
            addStudent(data[i]);
        }

    })
}

//在左侧列表生成一个班级
function createClass(id,name) {
    var node=$("<a id='"+id+"'><li class='list-group-item'>"+name+"</li></a>");
    node.click(function () {
        selectClass(id,name);
    });
    $("#class-select").append(node);
}

//修改班级名称
function editClassName() {
    var id=$("#class-id").text();
    var name=$("#change-class-name").find("input[name='name']").val();
    //验证班级名称
    if(name==""){
        alert("班级名称不能为空");
        return false;
    }
    //发送数据到后台
    $.get("http://139.199.200.112/dtpr/index.php?exam-teach-basic-modifyclass",{"id":id,"name":name},function (data) {
        data=JSON.parse(data);
        if(data.code==0){
            $("#class-name").text(name);
            $("#class-select").empty();
            initClassName();
        }else {
            alert(data.msg);
        }
    });
}

//删除班级
function removeClass() {
    id=$("#class-id").text();
    $.get("http://139.199.200.112/dtpr/index.php?exam-teach-basic-delclass",{"id":id},function (data) {
        data=JSON.parse(data);
        if(data.code==0){
            $(".col-xs-10").hide();
            $("#"+id).remove();
        }else {
            alert(data.msg);
        }
    })
}

//绑定提交拦截
$("form").submit(function () {
    if(testInputText($(this))){
        return true;
    }

    return false;
});

//给学生列表添加一个学生
function addStudent(stu) {
    var parent=$("#stu-members");

    var stuEle=$("<div class='col-sm-4 col-md-2 col-xs-6 member' data-toggle='modal' data-target='#member'>\n" +
        "                        <div class='thumbnail btn btn-default'>\n" +
        "                            <img src='/dtpr/images/picture.png' class='img-circle'>\n" +
        "                            <div class='caption'>\n" +
        "                                "+stu.opcname+"\n" +
        "                            </div>\n" +
        "                            <div class='account' hidden>"+stu.opcaccount+"</div>\n" +
        "                            <div class='number' hidden>"+stu.opcnumber+"</div>\n" +
        "                        </div>\n" +
        "                    </div>");

    stuEle.click(selectStudent);

    parent.append(stuEle);
}

//新增学生账号
function addStudentAccount() {
    if(!testInputText($("#stu-account"))){
        return;
    }

    var name=$("#add-stu-name");
    var account=$("#add-account");
    var amount=$("#test-add-amount");

    var json={};

    $.get("",json,function (data) {
        data=JSON.parse(data);

        if(data.code==0){
            addStudent(data);
        }else {
            alert(data.msg);
        }
    });
}

//选择学生
function selectStudent() {
    var parent=$(this);
    var modal=$("#member");

    modal.find(".stu-name .col-sm-6").text(parent.find(".caption").text());
    modal.find(".account .col-sm-6").text(parent.find(".account").text());
    modal.find(".test-amount .col-sm-6").text(parent.find(".number").text());
}

//新增班级
function addClass() {
    var formdata=new FormData($("#new-class")[0]);

    simpleSendAjax("http://139.199.200.112/dtpr/index.php?exam-teach-basic-addclass",formdata)
}

//加入班级
function joinClass() {
    var formdata=new FormData($("#join-class")[0]);

    simpleSendAjax("http://139.199.200.112/dtpr/index.php?exam-teach-basic-openclass");
}

//简单回调函数
function simpleSendAjax(url,formdata) {
    $.ajax({
        url:url,
        type:"get",
        processData: false,
        data:formdata,
        contentType : false,
        async: false,
        success: function (data) {
            var data=JSON.parse(data);
            if(data.code==0){
                location.reload();
            }else {
                alert(data.msg);
            }
        },
        error: function (data) {
            var data=JSON.parse(data);
            if(data.code==0){
                location.reload();
            }else {
                alert(data.msg);
            }
        }
    });

}